<template>
    <div id="add-edit-equipment">
        <el-dialog
            :title="titleCon"
            v-model="isUseDialog" 
            draggable 
            :close-on-click-modal="false" 
            width="769px" 
            :before-close="handleClose"
            :destroy-on-close="true"
            @close="clean"
        >
            <el-form :model="ruleForm" ref="ruleFormRef" size="default" label-width="80px" :rules="rules">
                <el-row :gutter="35">
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="设备名称" prop="v_Name">
                            <el-input v-model="ruleForm.v_Name" placeholder="设备名称" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="设备编码" prop="v_Code">
                            <el-input v-if="titleCon === '新增'" v-model="ruleForm.v_Code" placeholder="设备编码" clearable />
                            <el-input v-else readonly disabled v-model="ruleForm.v_Code" placeholder="设备编码" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="品牌" prop="v_BrandName">
                            <el-input v-model="ruleForm.v_BrandName" placeholder="品牌" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="规格型号" prop="v_SpecificationModel">
                            <el-input v-model="ruleForm.v_SpecificationModel" placeholder="规格型号" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="规格型号">
                            <el-select 
                                v-model="ruleForm.n_SpecType" 
                                filterable 
                                class="m-2" 
                                value-key="id"
                                placeholder="" 
                                @change="n_SpecTypeChange"
                            >
                                <el-option
                                    v-for="item in n_SpecTypeOption"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="主要参数" prop="v_MainParam">
                            <el-input v-model="ruleForm.v_MainParam" placeholder="主要参数" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="序号" prop="n_Sort">
                            <el-input type="number" v-model="ruleForm.n_Sort" placeholder="序号" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="备注">
                            <el-input type="textarea" v-model="ruleForm.v_Remark" placeholder="备注" clearable />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <el-button @click="cancelUseClick">取消</el-button>
                <el-button type="primary" :disabled="repeatDis" @click="submitForm(ruleFormRef)">确定</el-button>
            </template>
        </el-dialog>
    </div>
</template>
<script lang="ts">
import { defineComponent, ref, reactive , computed , toRef , toRefs , toRaw , onMounted , watch } from 'vue'
import { FormInstance, FormRules , ElMessage, ElButton, ElCol, ElDatePicker, ElDialog, ElDivider, ElForm, ElFormItem, ElInput, ElOption, ElRadio, ElRow, ElSelect, ElTable, ElTableColumn } from 'element-plus'
import { SafetyHatAdd , SafetyHatUpdate } from '/@/api/its/its.ts'
/*** 定义接口基础数据类型 */
interface formType {
    repeatDis: Boolean,
    ruleForm: ruleFormType,
}
/*** form 表单数据类型 */
type ruleFormType = {
    n_Id?: Number, // 组件id
    v_Name: String, // 设备名称
    v_Code: String, // 设备编码
    v_BrandName: String, // 品牌名称
    v_SpecificationModel: String, // 规格型号

    n_SpecType: Number, // 设备类型（0-智能帽衬，1-智能设备，2-执法仪）

    v_MainParam: String, // 主要参数
    n_Sort: Number, // 序号 默认值：1
    v_Remark: String, // 描述
}
export default defineComponent( {
    name: 'add-edit-equipment',
    components: {},
    props: {
        isUseDialog:{
            type: Boolean,
            default: false,
        },
        repairInfo: {
            type: String,
            default: ''
        }
    },
    setup(props,ctx){
        const titleCon = ref()
        const ruleFormRef = ref()

        /*** state 引用接口定义基础数据初始值 */
        const state = reactive < formType > ({
            repeatDis: false,
            ruleForm: {} as ruleFormType
        })

        /*** 基础验证 rules */
        const rules = reactive < FormRules > ({
            /*** 放置验证 */
            // 设备名称
            v_Name: [
				{ required: true, message: '请输入设备名称', trigger: 'blur' },
			],
            // 设备编码
            v_Code: [
				{ required: true, message: '请输入设备编码', trigger: 'blur' },
			],
            // 品牌名称
            v_BrandName: [
				{ required: true, message: '请输入品牌名称', trigger: 'blur' },
			],
            // 品牌名称
            v_SpecificationModel: [
				{ required: true, message: '请输入规格型号', trigger: 'blur' },
			],
            // 主要参数
            v_MainParam: [
				{ required: true, message: '请输入主要参数', trigger: 'blur' },
			],
            // 序号
            n_Sort: [
				{ required: true, message: '请输入序号', trigger: 'blur' },
			],
        })

        /*** n_SpecTypeChange 设备类型下拉改变事件 */
        const n_SpecTypeChange = (val:any) => {
            console.log(val,'val')
        }

        /*** handleClose 关闭按钮点击事件 */
        const handleClose = () => {
            cancelUseClick()
        }

        /*** cancelUseClick 取消事件 */
        const cancelUseClick = () => {
            ctx.emit('cancelUseClick')
        }

        /*** submitForm 提交事件 */
        const submitForm = (formEl: FormInstance | undefined) => {
            if(!formEl) return
            formEl.validate( async (valid) => { 
                if(valid){
                    let param = toRaw(state.ruleForm)
                    if(props.repairInfo === ''){
                        let res = await SafetyHatAdd(param)
                        if(res.success){
                            state.repeatDis = true
                            ctx.emit('controlUseClick','success')
                            ctx.emit('updateData')
                        }else{
                            ctx.emit('controlUseClick','')
                        }
                    }else{
                        let res = await SafetyHatUpdate(param)
                        if(res.success){
                            state.repeatDis = true
                            ctx.emit('controlUseClick','success')
                            ctx.emit('updateData')
                        }else{
                            ctx.emit('controlUseClick','')
                        }
                    }
                }else{
                    console.log('error submit!')
					return false
                }
            })
        }

        /*** clean 清空数据 */
        const clean = () => {
            ruleFormRef.value.resetFields()
        }

        /*** 监听isUseDialog的变化 */
        watch(() => props.isUseDialog,(newVal) => {
            if(newVal){
                state.repeatDis = false
                if(props.repairInfo !== ''){
                    titleCon.value = '编辑'
                    state.ruleForm = JSON.parse(props.repairInfo)
                }else{
                    titleCon.value = '新增'
                    state.ruleForm.v_Name = '' // 设备名称
                    state.ruleForm.v_Code = '' // 设备编码
                    state.ruleForm.v_BrandName = '' // 品牌名称
                    state.ruleForm.v_SpecificationModel = '' // 规格型号

                    state.ruleForm.n_SpecType = 0 // 设备类型（0-智能帽衬，1-智能设备，2-执法仪）

                    state.ruleForm.v_MainParam = '' // 主要参数
                    state.ruleForm.n_Sort = 1 // 序号 默认值：1
                    state.ruleForm.v_Remark = '' // 描述
                }
            }
        })

        /*** isUseDialog 响应弹窗的关闭  */
        const isUseDialog = computed(() => {
            return props.isUseDialog
        })

        return {
            handleClose,
            submitForm,
            cancelUseClick,
            clean,
            ruleFormRef,
            titleCon,
            ...toRefs(state),
            isUseDialog,
            rules,
            n_SpecTypeChange,
            n_SpecTypeOption: [
                {label: '智能帽衬', value: 0},
                {label: '智能设备', value: 1},
                {label: '执法仪', value: 2},
            ]
        }
    },
} )
</script>
<style scoped lang="scss">
#add-edit-equipment{
    :deep(.el-select){
        width: 100%;
    }
    :deep(.el-input){
        width: 100%;
    }
}
</style>